
typedef struct __S_Layer S_Layer;

struct __S_Layer {
    S_Layer*		m_UpLayer;
	S_Layer*		m_DownLayer;

	uint m_Layer_ID;

	uint		m_InNum;
	uint		m_OutNum;

	//-----------------Node------------------
	float**		m_Weigths;		//m_输入数量*m_输出数量
    float**     m_梯度;

	float*		m_Err;
    float*		m_Out;
	float*      m_Bias;
};

void f_aiCalc_NodeOut(S_Layer* Layers);


/*__kernel void f_aiSetInputData(__global float* inputData) {
    printf("S_Layer = ", sizeof(1));
}*/
__kernel void f_aiSetInputData(__global S_Layer* Layers, __global float* inputData) {
    size_t groupID = get_global_linear_id();
    
    Layers->m_Out[groupID] = inputData[groupID];
    
    //printf("groupID %f \n", Layers->m_Out[groupID]);
    if(groupID==0 && Layers->m_DownLayer) {
        //Layers->m_Out[Layers->m_OutNum-1] = 1;    //偏置节点
        //printf("A Layers %i\n", Layers->m_Layer_ID);
        ndrange_t ndrange1 = ndrange_1D(Layers->m_DownLayer->m_OutNum);
        
        void(^func_SamplingBlock)(void) = ^{ f_aiCalc_NodeOut(Layers->m_DownLayer); } ;
        int err_ret = enqueue_kernel(get_default_queue(), CLK_ENQUEUE_FLAGS_WAIT_KERNEL, ndrange1, func_SamplingBlock);

    }
}


void f_aiCalc_NodeOut(S_Layer* Layers) {
    size_t groupID = get_global_linear_id();
    
    S_Layer* upStrem = Layers->m_UpLayer;
    
    float outVar = 0.0;
    for(uint i=0; i<upStrem->m_OutNum; ++i){
        outVar += upStrem->m_Out[i] * Layers->m_Weigths[groupID][i];
        //printf("out %v4f \n", (float4)(outVar, upStrem->m_Out[i], Layers->m_Weigths[groupID][ upStrem->m_OutNum ], Layers->m_Layer_ID));
    }
    outVar += upStrem->m_Bias[ upStrem->m_OutNum-1 ] * Layers->m_Weigths[groupID][ upStrem->m_OutNum ];
    //printf("out %v4f \n", (float4)(outVar, upStrem->m_Bias[ upStrem->m_OutNum-1 ], Layers->m_Weigths[groupID][ upStrem->m_OutNum ], Layers->m_Layer_ID));

    Layers->m_Out[groupID] = 1.0 / (1.0+exp(-outVar));
    //printf("out %v4f \n", (float4)(Layers->m_Out[groupID], outVar, Layers->m_Weigths[groupID][ upStrem->m_OutNum ], upStrem->m_OutNum));
    printf("m_Out %f \n", outVar);

    if(groupID==0 && Layers->m_DownLayer) {
        ndrange_t ndrange1 = ndrange_1D(Layers->m_DownLayer->m_OutNum);
        
        void(^func_SamplingBlock)(void) = ^{ f_aiCalc_NodeOut(Layers->m_DownLayer); } ;
        int err_ret = enqueue_kernel(get_default_queue(), CLK_ENQUEUE_FLAGS_WAIT_KERNEL, ndrange1, func_SamplingBlock);
        
    }
}


